package server.business.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpResponse;

import java.util.Map;

import server.business.entity.ResultMessage;
import server.business.service.HttpServiceFactory;
import server.business.service.IHttpService;

import com.alibaba.fastjson.JSON;
import com.core.rest.AbstractHttpHandler;

public class IncreaseCoinsHandler extends AbstractHttpHandler implements Cloneable{
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    @Override
    protected void getData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void postData(Map<String, String> params, ChannelHandlerContext ctx) {}

    @Override
    protected void deleteData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void updateData(Map<String, String> params, ChannelHandlerContext ctx) {
        String text = "";
        
        IHttpService service = HttpServiceFactory.getService();
        
        ResultMessage rm = new ResultMessage(true);
        
        if(params == null || params.isEmpty()){
            
            rm.setSuccess(false);
            
        } else {
            
            String userId = params.get("userId");
            
            String currency = params.get("currency");
            
            long coins = Long.parseLong(params.get("coins"));
            
            boolean success = service.increaseCoins(userId, currency, coins);
            
            if(success){
                
                rm.setMessage("increase coins success");
                
            } else {
                
                rm.setMessage("increase coins faild");
                
                rm.setSuccess(false);
            }
            //text = JSON.toJSONString(list, true);
            
        }
        
        text = JSON.toJSONString(rm,true);
        
        FullHttpResponse response = wrapContent(text);
        
        sendResponse(ctx, response);
    
    }

    @Override
    public AbstractHttpHandler deepCopy() {
        
        AbstractHttpHandler httpHandler = null;
        
        try {
            
            httpHandler = (AbstractHttpHandler)(this.clone());
        
        } catch (CloneNotSupportedException e) {
            
            e.printStackTrace();
        }
        
        return httpHandler; 
    }


}
